package com.yf1706.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yf1706.bean.Applicant;
import com.yf1706.bean.CheckHolidayKind;
import com.yf1706.bean.Emp;
import com.yf1706.bean.NoticeInterviewStatusSetting;
import com.yf1706.bean.RecApplyRecord;
import com.yf1706.bean.RecTemplate;
import com.yf1706.bean.RecWaySetting;
import com.yf1706.service.ApplicantService;
import com.yf1706.service.NoticeInterviewStatusSettingService;
import com.yf1706.service.RecApplyRecordService;
import com.yf1706.service.RecTemplateService;
import com.yf1706.service.RecWaySettingService;
import com.yf1706.vo.ApplicantVO;
import com.yf1706.vo.CheckCalendarAffairVO;

@Controller
@RequestMapping("rec")
//controller负责的工作是接收客户端传过来的请求，并响应.controller所需要用到的就交给service去做
public class RecController {
	@Autowired	//自动装配
	//招聘申请单的service
	//controller所需要用到的就交给service
	private RecApplyRecordService rars;
	//招聘流程模板的service
	@Autowired
	private RecTemplateService rts;
	@Autowired
	private ApplicantService as;
	@Autowired 
	private RecWaySettingService rwss;
	@Autowired
	private NoticeInterviewStatusSettingService nisss;
	
	
	
	/**
	 * 进入招聘管理页面
	 * */
	@RequestMapping("rec")
	public String rec() {
		System.out.println("进入rec");
		return "yjw/rec_back_home";
	}
	
	/**
	 * 一、进入招聘申请单页面********************************
	 * */
	@RequestMapping("recApply")
	public String recApply() {
		System.out.println("进入recApply");
		return "yjw/rec_apply";
	}
	
	/**
	 * 1.1查找所有招聘申请单
	 * */
	@RequestMapping("queryAllRecApplyRecord")
	@ResponseBody
	public Map<String,Object> queryAllRecApplyRecord(int page,int rows){
		//分页，page页数，row是行数
		Page<RecApplyRecord> pages = PageHelper.startPage(page, rows);
		//把RecApplyRecord放到list集合中，通过rars调用queryAllRecApplyRecord方法，把这个方法放到list中
		List<RecApplyRecord> list = rars.queryAllRecApplyRecord();
		//遍历RecApplyRecord，把其中的数据输出
		for (Iterator<RecApplyRecord> it = list.iterator(); it.hasNext();) {
			RecApplyRecord rar = it.next();
			System.out.println(rar);
		}
		//把总页数pages放到total中
		long total = pages.getTotal();
		//定义一个新集合map
		Map<String, Object> map = new HashMap<String, Object>();
		//把总页数total、行数rows(即list)放到新集合中
		map.put("total", total);
		map.put("rows", list);
		//返回新集合map
		return map;
	}
	/**
	 * 1.2新增招聘申请单信息
	 * */
	@RequestMapping("addRecApplyRecord")
	@ResponseBody
	public void addHolidayKinds(HttpServletRequest request, HttpServletResponse response){
		//接收传过来的json数据
		 String jsondata=request.getParameter("jsondata");
		 System.out.println(jsondata);
		 //将json格式的数据转换为实体对象，然后存储为集合形式
		 List<RecApplyRecord> list = JSONObject.parseArray(jsondata, RecApplyRecord.class);
		 //将集合中的第一条对象存到通过service方法存储
		 rars.addRecApplyRecord(list.get(0));
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	
	/**
	 * 1.3修改招聘申请单信息
	 * */
	@RequestMapping("updateRecApplyRecord")
	@ResponseBody
	public void updateRecApplyRecord(HttpServletRequest request, HttpServletResponse response){
		//接收传过来的json数据
		 String jsondata=request.getParameter("jsondata");
		 System.out.println(jsondata);
		 //将json格式的数据转换为实体对象，然后存储为集合形式
		 List<RecApplyRecord> list = JSONObject.parseArray(jsondata, RecApplyRecord.class);
		 //将集合中的第一条对象存到通过service方法存储
		 rars.updateRecApplyRecord(list.get(0));
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	
	/**
	 * 1.4删除招聘申请单信息
	 * */
	@RequestMapping("deleteRecApplyRecord")
	@ResponseBody
	public void deleteHolidayKinds(HttpServletRequest request, HttpServletResponse response){
		 String jsondata=request.getParameter("jsondata");
		 //将json转换成int类型的集合
		 List<Integer> list = (List<Integer>) JSONObject.parse(jsondata);	
		 //循环删除id
       for (Integer o : list) {
    	   //o.intValue()代表将数值强转为int
    	   rars.deleteRecApplyRecord(o.intValue());
	}
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	
	/**
	 * 1.5模糊查询招聘申请单信息
	 * */
	@RequestMapping("searchRecApplyRecord")
	@ResponseBody
	public List<RecApplyRecord> searchRecApplyRecord(String search_jsondata){
		System.out.println(search_jsondata);
		List<RecApplyRecord> list = rars.getSearchRecApplyRecordList(search_jsondata);
		return list;
	}
	
	
	/**
	 * 二、进入招聘流程模板页面********************************
	 * */
	@RequestMapping("recTemplate")
	public String recTemplate() {
		System.out.println("进入recTemplate");
		return "yjw/rec_template";
	}

	
	/**
	 * 2.1查找所有招聘流程模板
	 * */
	@RequestMapping("queryAllRecTemplate")
	@ResponseBody
	public Map<String, Object> queryAllRecTemplate(int page,int rows){
		Page<RecTemplate> pages = PageHelper.startPage(page, rows);
		List<RecTemplate> list = rts.queryAllRecTemplate();
		for(Iterator<RecTemplate> it = list.iterator();it.hasNext();) {
			RecTemplate rt = it.next();
			System.out.println(rt);
		}
		long total = pages.getTotal();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	
	/**
	 * 2.2模糊查询招聘招聘流程模板
	 * */
	@RequestMapping("searchRecTemplate")
	@ResponseBody
	public List<RecTemplate> searchRecTemplate(String search_jsondata){
		System.out.println(search_jsondata);
		List<RecTemplate> list = rts.getSearchRecTemplate(search_jsondata);
		return list;
	}
	
	/**
	 * 2.3新增招聘流程模板
	 * */
	@RequestMapping("addRecTemplate")
	@ResponseBody
	public void addRecTemplate(HttpServletRequest request, HttpServletResponse response){
		//接收传过来的json数据
		 String jsondata=request.getParameter("jsondata");
		 System.out.println(jsondata);
		 //将json格式的数据转换为实体对象，然后存储为集合形式
		 List<RecTemplate> list = JSONObject.parseArray(jsondata, RecTemplate.class);
		 //将集合中的第一条对象存到通过service方法存储
		 rts.addRecTemplate(list.get(0));
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	
	/**
	 * 2.4修改招聘流程模板
	 * */
	@RequestMapping("updateRecTemplate")
	@ResponseBody
	public void updateRecTemplate(HttpServletRequest request, HttpServletResponse response){
		//接收传过来的json数据
		 String jsondata=request.getParameter("jsondata");
		 System.out.println(jsondata);
		 //将json格式的数据转换为实体对象，然后存储为集合形式
		 List<RecTemplate> list = JSONObject.parseArray(jsondata, RecTemplate.class);
		 //将集合中的第一条对象存到通过service方法存储
		 rts.updateRecTemplate(list.get(0));
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	
	/**
	 * 2.5删除招聘流程模板
	 * */
	@RequestMapping("deleteRecTemplate")
	@ResponseBody
	public void deleteRecTemplate(HttpServletRequest request, HttpServletResponse response){
		 String jsondata=request.getParameter("jsondata");
		 //将json转换成int类型的集合
		 List<Integer> list = (List<Integer>) JSONObject.parse(jsondata);	
		 //循环删除id
       for (Integer o : list) {
    	   //o.intValue()代表将数值强转为int
    	   rts.deleteRecTemplate(o.intValue());
	}
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	
	
	/**
	 * 三、进入求职人员登记页面********************************
	 * */
	@RequestMapping("appRegister")
	public String applicant() {
		System.out.println("进入appRegister");
		return "yjw/app_register";
	}
	
	/**
	 * 3.1查找所有求职人员
	 * */
	@RequestMapping("queryAllAppRegister")
	@ResponseBody
	public Map<String, Object> queryAllAppRegister(int page,int rows){
		Page<Applicant> pages = PageHelper.startPage(page, rows);
		List<Applicant> list = as.queryAllApplicant();
		for(Iterator<Applicant> it = list.iterator();it.hasNext();) {
			Applicant rt = it.next();
			System.out.println(rt);
		}
		long total = pages.getTotal();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	/**
	 * 3.2模糊查询求职人员
	 * */
	@RequestMapping("searchAppRegister")
	@ResponseBody
	public List<Applicant> searchAppRegister(String search_jsondata){
		System.out.println(search_jsondata);
		List<Applicant> list = as.getSearchAppRegister(search_jsondata);
		return list;
	}
	
	/**
	 * 3.3新增求职人员
	 * */
	@RequestMapping("addAppRegister")
	@ResponseBody
	public void addAppRegister(HttpServletRequest request, HttpServletResponse response){
		//接收传过来的json数据
		 String jsondata=request.getParameter("jsondata");
		 System.out.println(jsondata);
		 //将json格式的数据转换为实体对象，然后存储为集合形式
		 List<ApplicantVO> list = JSONObject.parseArray(jsondata, ApplicantVO.class);
		 //将集合中的第一条对象存到通过service方法存储
		 as.addAppRegister(list.get(0));
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
			
	}
	
	//3.3.1查询招聘方式（求职人员里面的）
	@RequestMapping("queryAllRecWaySetting")
	@ResponseBody
	public  List<RecWaySetting> queryAllRecWaySetting(){
        List<RecWaySetting> list= rwss.getRecWaySettingList();
		return list;
	}
	
	//3.3.2查询通知面试状态（求职人员里面的）
	@RequestMapping("queryAllNoticeInterviewStatus")
	@ResponseBody
	public  List<NoticeInterviewStatusSetting> queryAllNoticeInterviewStatus(){
        List<NoticeInterviewStatusSetting> list= nisss.queryALLNoticeInterviewStatusSetting();
		return list;
	}
	
	/**
	 * 3.4修改招求职人员
	 * */
	@RequestMapping("updateAppRegister")
	@ResponseBody
	public void updateAppRegister(HttpServletRequest request, HttpServletResponse response){
		//接收传过来的json数据
		 String jsondata=request.getParameter("jsondata");
		 System.out.println(jsondata);
		 //将json格式的数据转换为实体对象，然后存储为集合形式
		 List<ApplicantVO> list = JSONObject.parseArray(jsondata, ApplicantVO.class);
		 //将集合中的第一条对象存到通过service方法存储
		 as.updateAppRegister(list.get(0));
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	
	/**
	 * 2.5删除求职人员
	 * */
	@RequestMapping("deleteAppRegister")
	@ResponseBody
	public void deleteAppRegister(HttpServletRequest request, HttpServletResponse response){
		 String jsondata=request.getParameter("jsondata");
		 //将json转换成int类型的集合
		 List<Integer> list = (List<Integer>) JSONObject.parse(jsondata);	
		 //循环删除id
       for (Integer o : list) {
    	   //o.intValue()代表将数值强转为int
    	   as.deleteAppRegister(o.intValue());
	}
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	
	
	
	
}
